package gatoropoly.ui;

import static java.lang.Math.min;

import gatoropoly.game.Gatoropoly;
import gatoropoly.game.Estate;

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;

import javax.swing.JPanel;
import javax.swing.JPopupMenu;


public class EstateView extends JPanel {

	private static final long serialVersionUID = 6009335074727417445L;

	private static final double HEADER_SIZE = 0.25f;

	private static final Rectangle2D[] HEADERS = {
			new Rectangle2D.Double(0, 0, 1, HEADER_SIZE),
			new Rectangle2D.Double(1 - HEADER_SIZE, 0, HEADER_SIZE, 1),
			new Rectangle2D.Double(0, 1 - HEADER_SIZE, 1, HEADER_SIZE),
			new Rectangle2D.Double(0, 0, HEADER_SIZE, 1) };

	private static final Rectangle2D BACKGROUND = new Rectangle2D.Double(0, 0,
			1, 1);

	private final JPopupMenu menu;

	int rotation;

	final Estate item;

	private final Gatoropoly client;

	public EstateView(Gatoropoly client, Estate item) {
		this.item = item;
		this.client = client;
		
		menu = new JPopupMenu(item.getName());
		PeerSupport<Estate> support = new PeerSupport<Estate>(client, item);
		menu.add(support.getAction(Estate.Action.MORTGAGE));
		menu.add(support.getAction(Estate.Action.UNMORTGAGE));
		menu.add(support.getAction(Estate.Action.BUY_HOUSE));
		menu.add(support.getAction(Estate.Action.SELL_HOUSE));
		menu.add(support.getAction(Estate.Action.BUY_HOTEL));
		menu.add(support.getAction(Estate.Action.SELL_HOTEL));
		setComponentPopupMenu(menu);

		setLayout(new BorderLayout());
		setDoubleBuffered(false);
		setBackground(item.getBackgroundColor());
	}

	public void paintComponent(Graphics gr) {
		Dimension d = getSize();
		AffineTransform t = AffineTransform.getScaleInstance(d.width, d.height);
		Shape background = t.createTransformedShape(BACKGROUND);
		Shape header = t.createTransformedShape(HEADERS[min(HEADERS.length,
				rotation)]);

		Graphics2D g = (Graphics2D) gr;
		float w = getWidth();
		float h = getHeight();

		Color color = (item.isMortgaged() ? Color.BLACK : (item.canBeOwned()
				&& item.getOwner() == null ? Color.WHITE : item
				.getBackgroundColor()));
		if (color != null && background != null) {
			g.setColor(color);
			g.fill(background);
		}

		color = item.getColor();
		if (color != null && header != null) {
			g.setColor(color);
			g.fill(header);
		}

		if (background != null) {
			g.setStroke(new BasicStroke(2));
			g
					.setColor((item.getOwner() != null
							&& item.getOwner() == client.getState().self() ? Color.WHITE
							: Color.BLACK));
			g.draw(background);
		}

		Image icon = item.getIcon();
		if (icon != null)
			g.drawImage(icon, (getWidth() - icon.getWidth(this)) / 2,
					(getHeight() - icon.getHeight(this)) / 2, this);

		if (item.getName() != null) {
			int sw = g.getFontMetrics().stringWidth(item.getName());
			g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
					RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
			g.setColor(item.isMortgaged() ? Color.WHITE : Color.BLACK);
			g.drawString(item.getName(), Math.max(0, (w - sw) / 2), h / 2);
		}
	}

}
